import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

const isLogin = uni.getStorageSync('isLogin') || false;
const userInfo = uni.getStorageSync('userInfo') || null;

export default new Vuex.Store({
	state: {
		configComplete: false,
		isLogin,
		userInfo,
		globalConfig: {
			login_bc: 'http://127.0.0.1:5500/images/login_bc.png',
			logo: ''
		}
	},
	getters: {
		getState: (state) => (key) => {
			return state[key]
		},
		isLogin(state) {
			return state.isLogin
		},
		userInfo(state) {
			return state.userInfo
		},
		globalConfig(state) {
			return state.globalConfig
		},
		configComplete(state) {
			return state.configComplete
		}
	},
	mutations: {
		setState(state, data) {
		    for (let key in data) {
		        state[key] = data[key]
				if(key !== 'pageTips') {
					uni.setStorage({
						key,
						data: data[key]
					})
				}
		    }
		},
		logout(state) {
			uni.clearStorage();
			state.isLogin = false;
			state.userInfo = null
		} 
	},
	actions: {

	}
})
